package com.mihuo.controller.sys;

import com.mihuo.entity.dto.JobOperationDTO;
import org.quartz.*;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * quartz 定时任务管理界面api
 */
@RestController
@RequestMapping("/sys/quartz")
public class QuartzManagerController {

    @Resource
    private Scheduler scheduler;

    /**
     * 获取所有任务列表
     */
    @GetMapping("/jobs")
    public Map<String, Object> getAllJobs() throws SchedulerException {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> jobList = new ArrayList<>();

        // 遍历所有任务组
        for (String groupName : scheduler.getJobGroupNames()) {
            // 遍历组内所有任务
            for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
                JobDetail jobDetail = scheduler.getJobDetail(jobKey);
                List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);

                for (Trigger trigger : triggers) {
                    Map<String, Object> jobMap = new HashMap<>();
                    jobMap.put("jobName", jobKey.getName());
                    jobMap.put("jobGroup", jobKey.getGroup());
                    jobMap.put("jobClass", jobDetail.getJobClass().getName());
                    jobMap.put("description", jobDetail.getDescription());

                    // 触发器信息
                    jobMap.put("triggerName", trigger.getKey().getName());
                    jobMap.put("triggerGroup", trigger.getKey().getGroup());
                    jobMap.put("triggerState", scheduler.getTriggerState(trigger.getKey()).toString());

                    // Cron触发器特定信息
                    if (trigger instanceof CronTrigger) {
                        CronTrigger cronTrigger = (CronTrigger) trigger;
                        jobMap.put("cronExpression", cronTrigger.getCronExpression());
                        jobMap.put("timeZone", cronTrigger.getTimeZone().getID());
                    }

                    jobMap.put("startTime", trigger.getStartTime());
                    jobMap.put("endTime", trigger.getEndTime());
                    jobMap.put("previousFireTime", trigger.getPreviousFireTime());
                    jobMap.put("nextFireTime", trigger.getNextFireTime());

                    jobList.add(jobMap);
                }
            }
        }

        result.put("success", true);
        result.put("data", jobList);
        result.put("total", jobList.size());
        return result;
    }

    /**
     * 暂停任务 - 改为POST请求接收JSON参数
     */
    @PostMapping(value = "/pauseJob", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> pauseJob(@RequestBody JobOperationDTO requestDTO) throws SchedulerException {
        Map<String, Object> result = new HashMap<>();
        // 从DTO对象中获取参数
        JobKey jobKey = new JobKey(requestDTO.getJobName(), requestDTO.getJobGroup());
        scheduler.pauseJob(jobKey);
        result.put("success", true);
        result.put("message", "任务已暂停");
        return result;
    }

    /**
     * 恢复任务 - 改为POST请求接收JSON参数
     */
    @PostMapping(value = "/resumeJob", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> resumeJob(@RequestBody JobOperationDTO requestDTO) throws SchedulerException {
        Map<String, Object> result = new HashMap<>();
        JobKey jobKey = new JobKey(requestDTO.getJobName(), requestDTO.getJobGroup());
        scheduler.resumeJob(jobKey);
        result.put("success", true);
        result.put("message", "任务已恢复");
        return result;
    }

    // 同理修改 triggerJob, deleteJob 等方法...
    @PostMapping(value = "/triggerJob", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> triggerJob(@RequestBody JobOperationDTO requestDTO) throws SchedulerException {
        Map<String, Object> result = new HashMap<>();
        JobKey jobKey = new JobKey(requestDTO.getJobName(), requestDTO.getJobGroup());
        scheduler.triggerJob(jobKey);
        result.put("success", true);
        result.put("message", "任务已触发执行");
        return result;
    }

    @PostMapping(value = "/deleteJob", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> deleteJob(@RequestBody JobOperationDTO requestDTO) throws SchedulerException {
        Map<String, Object> result = new HashMap<>();
        JobKey jobKey = new JobKey(requestDTO.getJobName(), requestDTO.getJobGroup());
        scheduler.deleteJob(jobKey);
        result.put("success", true);
        result.put("message", "任务已删除");
        return result;
    }
}